package com.penghaonan.appframework.utils.monitor;

import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;

import com.penghaonan.appframework.AppDelegate;
import com.penghaonan.appframework.utils.BroadcastUtils;
import com.penghaonan.appframework.utils.NetworkUtils;

public class NetworkMonitor extends AMonitor<Boolean> {

    private final BroadcastUtils.ActionReceiver receiver = new BroadcastUtils.ActionReceiver() {
        @Override
        protected void onActionReceive(String action, Intent intent) {
            notifyListeners();
        }
    };

    public NetworkMonitor() {
        BroadcastUtils.register(receiver, new String[]{ConnectivityManager.CONNECTIVITY_ACTION, WifiManager.WIFI_STATE_CHANGED_ACTION, WifiManager.NETWORK_STATE_CHANGED_ACTION});
    }

    @Override
    public Boolean getValue() {
        return NetworkUtils.isNetworkConnected() && NetworkUtils.getNetype() == ConnectivityManager.TYPE_WIFI;
    }

    @Override
    public void release() {
        super.release();
        BroadcastUtils.unregister(receiver);
    }
}
